package com.yc.ioc;

public class Demo01 {

    public static void main(String[] args) {
        Computer computer = new Computer();
        // Spring 通过注入 DI 将打印机注入到computer内
        Printer p1 = new BlackWhitePrinter();
        Printer p2 = new ColorPrinter();

        computer.printer = p1; // DI 依赖注入
        computer.print("Spring Demo");

        computer.printer = p2; // DI 依赖注入
        computer.print("Spring Demo");

        Storage s1 = new Upan();
        Storage s2 = new Yingpan();

        computer.storage = s1;
        computer.storage = s2;
    }
}

class Computer{
    // 不在程序中直接创建对象，将对象的控制器权交由 IOC 容器，就是控制反转
    Printer printer;    // = new XxxxPrinter();
    void print(String text){
        printer.print(text);
    }

    Storage storage;
    void save(Object data){
        storage.save(data);
    }
}

// 存储介质接口，请定义 U盘 和 硬盘
interface Storage{
    void save(Object data);
}

class Upan implements Storage{

    public void save(Object data) {
        System.out.println("U盘存储" + data);
    }
}

class Yingpan implements Storage{

    public void save(Object data) {
        System.out.println("硬盘存储" + data);
    }
}

interface Printer{
    void print(String text);
}

class BlackWhitePrinter implements Printer{

    public void print(String text) {
        System.out.println("黑色文字:" + text);
    }
}

class ColorPrinter implements Printer{

    public void print(String text) {
        System.out.println("彩色文字:" + text);
    }
}
